﻿package mortal.game.view.playerSystem.view
{
    import Message.DB.Tables.*;
    import __AS3__.vec.*;
    import com.mui.controls.*;
    import com.mui.utils.*;
    import extend.language.*;
    import flash.events.*;
    import flash.utils.*;
    import mortal.component.gconst.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.resource.tableConfig.*;
    import mortal.game.view.common.item.*;
    import mortal.game.view.common.util.*;
    import mortal.game.view.skill.*;
    import mortal.mvc.core.*;

    public class PlayerTargetPanel extends GSprite
    {
        private var _attribute:Vector.<String>;
        private var _targetDic:Dictionary;

        public function PlayerTargetPanel()
        {
            this._attribute = new Vector.<String>;
            this._targetDic = new Dictionary();
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this._attribute.push("attack", "life", "physDefense", "magicDefense", "penetration", "jouk", "hit", "crit", "toughness", "block", "expertise");
            this.createTargetList();
            this.addEvent();
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            this.removeEvent();
            this._targetDic = new Dictionary();
            this._attribute.length = 0;
            super.disposeImpl(param1);
            return;
        }// end function

        protected function addEvent() : void
        {
            NetDispatcher.addCmdListener(ServerCommand.updateEquipMent, this.updateEquipByType);
            NetDispatcher.addCmdListener(ServerCommand.TargetSuitGroupUpdate, this.updateSuitGroupTarget);
            Dispatcher.addEventListener(EventName.ForgingStrengTargetUpdate, this.updateStrengTarget);
            Dispatcher.addEventListener(EventName.ForgingGemCombatTargetUpdate, this.updateForgingGemCombatTarget);
            return;
        }// end function

        protected function removeEvent() : void
        {
            NetDispatcher.removeCmdListener(ServerCommand.updateEquipMent, this.updateEquipByType);
            NetDispatcher.removeCmdListener(ServerCommand.TargetSuitGroupUpdate, this.updateSuitGroupTarget);
            Dispatcher.removeEventListener(EventName.ForgingStrengTargetUpdate, this.updateStrengTarget);
            Dispatcher.removeEventListener(EventName.ForgingGemCombatTargetUpdate, this.updateForgingGemCombatTarget);
            return;
        }// end function

        private function updateEquipByType(param1:Object) : void
        {
            this.updateStrengTarget();
            this.updateForgingGemCombatTarget();
            this.updateSuitGroupTarget();
            return;
        }// end function

        private function updateStrengTarget(event:DataEvent = null) : void
        {
            this.updateTargetByType("strengthen");
            return;
        }// end function

        private function updateForgingGemCombatTarget(event:DataEvent = null) : void
        {
            this.updateTargetByType("jewel");
            return;
        }// end function

        private function updateSuitGroupTarget(event:DataEvent = null) : void
        {
            this.updateTargetByType("suitdefense");
            return;
        }// end function

        private function createTargetList() : void
        {
            var _loc_1:BaseItem = null;
            var _loc_2:int = 0;
            while (_loc_2 < 4)
            {
                
                _loc_1 = UICompomentPool.getUICompoment(BaseItem);
                _loc_1.setItemStyle(34, null, 2, 2);
                this.addChild(_loc_1);
                _loc_1.x = _loc_2 * 45;
                _loc_1.y = 0;
                this._targetDic[this.getTypeByIndex(_loc_2)] = _loc_1;
                this.updateTargetByType(this.getTypeByIndex(_loc_2));
                this.pushUIToDisposeVec(_loc_1);
                _loc_2++;
            }
            return;
        }// end function

        private function getTypeByIndex(param1:int) : String
        {
            var _loc_2:String = null;
            switch(param1)
            {
                case 0:
                {
                    _loc_2 = "strengthen";
                    break;
                }
                case 1:
                {
                    _loc_2 = "suitdefense";
                    break;
                }
                case 2:
                {
                    _loc_2 = "medal";
                    break;
                }
                case 3:
                {
                    _loc_2 = "jewel";
                    break;
                }
                default:
                {
                    break;
                }
            }
            return _loc_2;
        }// end function

        private function getMedalTarget()
        {
            (this._targetDic["medal"] as BaseItem).setQualiy(1, "Target_");
            (this._targetDic["medal"] as BaseItem).setAmountLabel("");
            (this._targetDic["medal"] as BaseItem).source = "TargetMedal.jpg";
            (this._targetDic["medal"] as BaseItem).bitmap.filters = [FilterConst.colorFilter2];
            return Language.getString(35326);
        }// end function

        private function openMedal(event:MouseEvent) : void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.MedalOpen));
            return;
        }// end function

        private function getAttrValue(param1:TEquipTarget) : String
        {
            var _loc_3:Boolean = false;
            var _loc_4:int = 0;
            var _loc_2:String = "";
            while (_loc_4 < this._attribute.length)
            {
                
                if (param1.hasOwnProperty(this._attribute[_loc_4]) && param1[this._attribute[_loc_4]] > 0)
                {
                    if (!_loc_3)
                    {
                        _loc_2 = _loc_2 + (GameDefConfig.instance.getFightAttributeName(this._attribute[_loc_4]) + ": +" + param1[this._attribute[_loc_4]] + "     ");
                    }
                    else
                    {
                        _loc_2 = _loc_2 + (GameDefConfig.instance.getFightAttributeName(this._attribute[_loc_4]) + ": +" + param1[this._attribute[_loc_4]] + "\n");
                    }
                    _loc_3 = !_loc_3;
                }
                _loc_4++;
            }
            return _loc_2;
        }// end function

        private function getStrengthenTarget() : String
        {
            var _loc_3:String = null;
            var _loc_4:TEquipTarget = null;
            var _loc_6:int = 0;
            var _loc_1:* = EquipmentUtil.getStrengTargetLevel();
            var _loc_2:* = PlayerTargetConfig.instance.getTargetListByType("strengthen");
            var _loc_5:* = this._targetDic["strengthen"] as BaseItem;
            while (_loc_6 < _loc_2.length)
            {
                
                _loc_4 = _loc_2[_loc_6];
                if (_loc_4.level == _loc_1)
                {
                    break;
                }
                else
                {
                    _loc_4 = null;
                }
                _loc_6++;
            }
            if (_loc_4)
            {
                if (_loc_6 == (_loc_2.length - 1))
                {
                    _loc_3 = "<font color=\'#1ccc31\'>" + Language.getString(30155) + " : " + Language.getString(35327) + _loc_4.level + "</font>\n" + this.getAttrValue(_loc_4);
                    _loc_5.setAmountLabel("13/13");
                }
                else
                {
                    _loc_3 = "<font color=\'#1ccc31\'>" + Language.getString(30156) + Language.getString(35327) + _loc_4.level + "</font>\n" + this.getAttrValue(_loc_4) + " \n<font color=\'#1ccc31\'>" + Language.getString(30157) + Language.getString(35327) + (_loc_2[(_loc_6 + 1)] as TEquipTarget).level + "</font> (" + EquipmentUtil.getEquipsByStrengLevel((_loc_2[(_loc_6 + 1)] as TEquipTarget).level).length + "/13)\n" + this.getAttrValue(_loc_2[(_loc_6 + 1)]);
                    _loc_5.setAmountLabel("<font color=\'#1ccc31\'>" + EquipmentUtil.getEquipsByStrengLevel((_loc_2[(_loc_6 + 1)] as TEquipTarget).level).length + "</font>/13");
                }
                _loc_5.source = "TargetStrenge_1.png";
            }
            else
            {
                _loc_4 = _loc_2[0] as TEquipTarget;
                _loc_3 = "<font color=\'#1ccc31\'>" + Language.getString(30157) + Language.getString(35327) + _loc_4.level + "</font> (" + EquipmentUtil.getEquipsByStrengLevel(_loc_4.level).length + "/13)\n" + this.getAttrValue(_loc_4);
                _loc_5.setAmountLabel("<font color=\'#1ccc31\'>" + EquipmentUtil.getEquipsByStrengLevel(_loc_4.level).length + "</font>/13");
                _loc_5.source = "TargetStrenge_0.png";
            }
            return _loc_3;
        }// end function

        private function getForgingGemTarget() : String
        {
            var _loc_3:String = null;
            var _loc_4:TEquipTarget = null;
            (this._targetDic["jewel"] as BaseItem).setAmountLabel("");
            (this._targetDic["jewel"] as BaseItem).source = "TargetGem_0.png";
            (this._targetDic["jewel"] as BaseItem).setQualiy(1, "Target_");
            return Language.getString(35326);
            if (_loc_4)
            {
                if (_loc_5 == (_loc_2.length - 1))
                {
                    _loc_3 = "<font color=\'#1ccc31\'>" + Language.getString(30155) + Language.getString(35330) + _loc_4.combat + "</font>\n" + this.getAttrValue(_loc_4);
                }
                else
                {
                    _loc_3 = "<font color=\'#1ccc31\'>" + Language.getString(30156) + Language.getString(35331) + _loc_4.combat + "</font>\n" + this.getAttrValue(_loc_4) + " \n<font color=\'#1ccc31\'>" + Language.getString(30157) + Language.getString(35331) + (_loc_2[(_loc_5 + 1)] as TEquipTarget).combat + "</font> (" + _loc_1 + ")\n" + this.getAttrValue(_loc_2[(_loc_5 + 1)]);
                }
                (this._targetDic["jewel"] as BaseItem).source = "TargetGem_1.png";
            }
            else
            {
                _loc_3 = "<font color=\'#1ccc31\'>" + Language.getString(30157) + Language.getString(35331) + (_loc_2[0] as TEquipTarget).combat + "</font> (" + _loc_1 + ")\n" + this.getAttrValue(_loc_2[0]);
                (this._targetDic["jewel"] as BaseItem).source = "TargetGem_0.png";
            }
            (this._targetDic["jewel"] as BaseItem).setAmountLabel("<font color=\'#1ccc31\'>" + _loc_1.toString() + "</font>");
            return _loc_3;
        }// end function

        private function getSuitGroupTarget() : String
        {
            var _loc_3:TEquipTarget = null;
            var _loc_4:ItemEquipInfo = null;
            var _loc_5:TSuit = null;
            var _loc_6:int = 0;
            var _loc_7:int = 0;
            var _loc_8:Boolean = false;
            var _loc_9:Array = null;
            var _loc_10:int = 0;
            var _loc_11:int = 0;
            var _loc_12:Array = null;
            var _loc_15:int = 0;
            var _loc_16:int = 0;
            (this._targetDic["suitdefense"] as BaseItem).setAmountLabel("");
            (this._targetDic["suitdefense"] as BaseItem).setQualiy(1, "Target_");
            (this._targetDic["suitdefense"] as BaseItem).source = "TargetSuit_0.png";
            return Language.getString(35326);
            _loc_14.source = "TargetSuit_" + _loc_15 + ".png";
            return "<font color=\'#1ccc31\'>" + _loc_2;
        }// end function

        private function getSuitGroupTarget2() : String
        {
            var _loc_2:String = null;
            var _loc_3:TEquipTarget = null;
            var _loc_4:ItemEquipInfo = null;
            var _loc_5:TSuit = null;
            var _loc_6:int = 0;
            var _loc_7:int = 0;
            var _loc_8:Boolean = false;
            var _loc_9:Array = null;
            var _loc_10:int = 0;
            var _loc_11:int = 0;
            var _loc_12:Array = null;
            var _loc_15:int = 0;
            var _loc_1:* = PlayerTargetConfig.instance.getTargetListByType("suitdefense");
            var _loc_13:* = Cache.instance.role.entityInfo.career;
            var _loc_14:* = this._targetDic["suitdefense"] as BaseItem;
            while (_loc_15 < _loc_1.length)
            {
                
                _loc_3 = _loc_1[_loc_15];
                _loc_4 = getSuilItemInfo(_loc_3, _loc_13);
                _loc_5 = ItemConfig.instance.getSuitById(_loc_4.suitId);
                if (Cache.instance.role.targetSuitDefense == _loc_5.id)
                {
                    _loc_8 = true;
                    break;
                }
                _loc_15++;
            }
            if (_loc_8)
            {
                _loc_9 = _loc_5.skill.split("#");
                _loc_10 = _loc_9[0];
                _loc_11 = _loc_9[1];
                _loc_6 = EquipmentUtil.getEquipmentsByLevelAndSuitGroup(_loc_4.level, _loc_4.suitId).length;
                _loc_7 = _loc_5.type.split("#").length;
                _loc_2 = Language.getStringByParam(30158, _loc_7, _loc_4.level, ColorConfig.instance.getItemColor(_loc_4.color).colorText, "", SkillUtil.getSkillDesc(_loc_10) + "\n" + SkillUtil.getSkillDesc(_loc_11));
                _loc_14.source = "TargetSuit_" + (_loc_4.level > 30 ? (50) : (_loc_4.level)) + ".png";
                _loc_3 = _loc_1[(_loc_15 + 1)];
                if (_loc_3)
                {
                    _loc_4 = getSuilItemInfo(_loc_3, _loc_13);
                    _loc_5 = ItemConfig.instance.getSuitById(_loc_4.suitId);
                    _loc_9 = _loc_5.skill.split("#");
                    _loc_10 = _loc_9[0];
                    _loc_11 = _loc_9[1];
                    _loc_7 = _loc_5.type.split("#").length;
                    _loc_6 = EquipmentUtil.getEquipmentsByLevelAndSuitGroup(_loc_4.level, _loc_4.suitId).length;
                    _loc_2 = Language.getString(30156) + _loc_2 + "\n<font color=\'#1ccc31\'>" + Language.getString(30157) + Language.getStringByParam(30158, _loc_7, _loc_4.level, ColorConfig.instance.getItemColor(_loc_4.color).colorText, "(" + _loc_6 + "/" + _loc_7 + ")", SkillUtil.getSkillDesc(_loc_10) + "\n" + SkillUtil.getSkillDesc(_loc_11));
                    _loc_14.setAmountLabel("<font color=\'#1ccc31\'>" + _loc_6 + "</font>/" + _loc_7);
                }
                else
                {
                    _loc_2 = Language.getString(30155) + " : " + _loc_2;
                    _loc_14.setAmountLabel("");
                }
            }
            else
            {
                _loc_3 = _loc_1[1];
                if (_loc_3)
                {
                    _loc_4 = getSuilItemInfo(_loc_3, _loc_13);
                    _loc_6 = EquipmentUtil.getEquipmentsByLevelAndSuitGroup(_loc_4.level, _loc_4.suitId).length;
                }
                if (_loc_6)
                {
                    _loc_14.source = "TargetSuit_1.png";
                }
                else
                {
                    _loc_3 = _loc_1[0] as TEquipTarget;
                    _loc_4 = getSuilItemInfo(_loc_3, _loc_13);
                    _loc_6 = EquipmentUtil.getEquipmentsByLevelAndSuitGroup(_loc_4.level, _loc_4.suitId).length;
                    _loc_14.source = "TargetSuit_0.png";
                }
                _loc_5 = ItemConfig.instance.getSuitById(_loc_4.suitId);
                _loc_7 = _loc_5.type.split("#").length;
                _loc_9 = _loc_5.skill.split("#");
                _loc_10 = _loc_9[0];
                _loc_11 = _loc_9[1];
                _loc_2 = Language.getString(30157) + Language.getStringByParam(30158, _loc_7, _loc_4.level, ColorConfig.instance.getItemColor(_loc_4.color).colorText, "(" + _loc_6 + "/" + _loc_7 + ")", SkillUtil.getSkillDesc(_loc_10) + "\n" + SkillUtil.getSkillDesc(_loc_11));
                _loc_14.setAmountLabel("<font color=\'#1ccc31\'>" + _loc_6 + "</font>/" + _loc_7 + "");
            }
            return "<font color=\'#1ccc31\'>" + _loc_2;
        }// end function

        public function updateTargetByType(param1:String) : void
        {
            var _loc_2:* = undefined;
            switch(param1)
            {
                case "strengthen":
                {
                    _loc_2 = this.getStrengthenTarget();
                    break;
                }
                case "jewel":
                {
                    _loc_2 = this.getForgingGemTarget();
                    break;
                }
                case "suitdefense":
                {
                    _loc_2 = this.getSuitGroupTarget();
                    break;
                }
                case "medal":
                {
                    _loc_2 = this.getMedalTarget();
                    break;
                }
                default:
                {
                    break;
                }
            }
            (this._targetDic[param1] as BaseItem).toolTipData = _loc_2;
            return;
        }// end function

        public static function getSuilItemInfo(param1:TEquipTarget, param2:int) : ItemEquipInfo
        {
            var _loc_3:ItemEquipInfo = null;
            var _loc_5:String = null;
            var _loc_4:* = param1.suitItemCode.split("#");
            for each (_loc_5 in _loc_4)
            {
                
                _loc_3 = ItemConfig.instance.getConfig(int(_loc_5)) as ItemEquipInfo;
                if (_loc_3.career == param2)
                {
                    break;
                }
            }
            return _loc_3;
        }// end function

    }
}
